﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace auxiliary_proj
{
    public partial class FormCreateVendor : Form
    {
        public FormCreateVendor()
        {
            InitializeComponent();
        }
        bool isAbbr = false, isState = false, isName = false, isAddress = false, isCity = false;
        string vendorAbbr, vendorName, vendorAddress, vendorCity, vendorState, vendorZip, vendorPhone;


        private void btnCreateVendor_Click(object sender, EventArgs e)
        {
            vendorAbbr = tbVendorAbbr.Text.Trim();
            vendorName = tbVendor_Name.Text;
            vendorAddress = tbVendor_Addr.Text.Trim();
            vendorCity = tbCity.Text.Trim();
            vendorState = tbState.Text;
            vendorZip = tbZipCode.Text;
            vendorPhone = tbPhone.Text;

            if ((isAbbr) && (isName) && (isAddress) && (isCity) && (isState))
            {
                Program.parseServerInfo(); //parse the server file

                //insert query
                string insert = "INSERT INTO `vendor`(`vendor_id`, `name`, `address`, `location`, `phone`) VALUES ('" + vendorAbbr.ToUpper() + "','" + vendorName + "', '" + vendorAddress + "', '" + vendorCity + ", " + vendorState.ToUpper() + " " + vendorZip + "', '" + vendorPhone + "')";
                if (Query.nonQuery(insert))
                {
                    MessageBox.Show("Vendor Created!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Failed To Create!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
            else MessageBox.Show("Please Complete the Form!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void abbr_textChanged(object sender, EventArgs e)
        {
            isAbbr = Parser.isMatch(sender, @"^[a-zA-Z]{3,5}$");
        }

        private void name_textChanged(object sender, EventArgs e)
        {
            isName = Parser.isMatch(sender, @"^[a-zA-Z\s-,&.]{3,}$");
        }

        private void address_textChanged(object sender, EventArgs e)
        {
            isAddress = Parser.isMatch(sender, @"^[a-zA-Z0-9\s-&.]{5,}$");
        }

        private void city_textChanged(object sender, EventArgs e)
        {
            isCity = Parser.isMatch(sender, @"^[a-zA-Z\s.-]{3,20}$");
        }

        private void state_textChanged(object sender, EventArgs e)
        {
            isState = Parser.isMatch(sender, @"^[a-zA-Z]{2}$");
        }
    }
}

